package com.astn.bks;

import com.astn.bks.util.BksConstants;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;

public class HomeTabHost extends TabActivity {

	private TabHost mTabHost;

	private void setupTabHost() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// construct the tabhost
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ctabhost);

		setupTabHost();
		mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
		initialTopProviderTab(new TextView(this));
		initialTopDestination(new TextView(this));
		initialMyRequest(new TextView(this));
	}

	private void setupTab(final View view, final String tag) {
		View tabview = createTabView(mTabHost.getContext(), tag);

		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(new TabContentFactory() {
					public View createTabContent(String tag) {
						return view;
					}
				});
		mTabHost.addTab(setContent);

	}

	private void initialTopProviderTab(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "Top provider");

		TabSpec setContent = mTabHost.newTabSpec("Top provider")
				.setIndicator(tabview)
				.setContent(new Intent(HomeTabHost.this, CTopProvider.class));
		mTabHost.addTab(setContent);

	}

	private void initialTopDestination(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "Top destination");

		TabSpec setContent = mTabHost
				.newTabSpec("Top destination")
				.setIndicator(tabview)
				.setContent(new Intent(HomeTabHost.this, CTopDestination.class));
		mTabHost.addTab(setContent);

	}

	private void initialMyRequest(final View view) {
		View tabview = createTabView(mTabHost.getContext(), "My request");

		TabSpec setContent = mTabHost.newTabSpec("My request")
				.setIndicator(tabview)
				.setContent(new Intent(HomeTabHost.this, CMyRequest.class));
		mTabHost.addTab(setContent);

	}

	private static View createTabView(final Context context, final String text) {
		View view = LayoutInflater.from(context)
				.inflate(R.layout.tabs_bg, null);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.optmenucustomer, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnPostRequest:
			// Select post request
			cPostRequest();
			return true;
		case R.id.mnHistory:
			// View history
			cHistory();
			return true;
		case R.id.mnMessage:
			// Message
			cMessage();
			return true;
		case R.id.mnSearch:
			// Search
			cSearch();
			return true;
		case R.id.mnPayment:
			// Payment infomation
			cPayment();
			return true;
		case R.id.mnHelp:
			// Help
			cHelp();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void cPostRequest() {
		Intent t = new Intent(HomeTabHost.this, PostRequest.class);
		startActivityForResult(t, BksConstants.POST_REQUEST);
	}

	private void cHistory() {

	}

	private void cMessage() {

	}

	private void cSearch() {

	}

	private void cPayment() {

	}

	private void cHelp() {

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

}